<?php
/**
 * Created by JetBrains PhpStorm.
 * User: ivan
 * Date: 8/30/12
 * Time: 5:02 PM
 * To change this template use File | Settings | File Templates.
 */
 
class Page_Be_Cms_GeneralSkinFiles extends Page {

	/**
	 * @var Db_Cms_GeneralSkinFiles
	 */
	protected $tableObject;

	function __construct() {
        self::$TPL->appendJsFile('admin/skins');
        self::$TPL->appendCssFile('admin/skins');
        $this->idTableObject = post('id', 'int') ?  post('id', 'int') : get('id', 'int');
        $this->actionsTableObject = array(
            'addEditAction',
            'saveAction',
            'saveGeneralSkinFilesPositionAction');
        $this->classTableObject = 'Db_Cms_GeneralSkinFiles';
        parent::__construct();
    }

    public function defaultAction() {
        if (perm('GENERAL_SKIN_FILES_EDIT')) {
            $this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit');
        }
        $allFiles =  Db_Cms_GeneralSkinFiles::getAllGeneralFiles();
        self::$TPL->assign('files', $allFiles);
        self::$TPL->setBodyTemplate('general_skin_files_list');
        return array(
            'type' => 'display'
        );
    }

     public function addEditAction() {
         perm('GENERAL_SKIN_FILES_EDIT', self::$PAGE['p_url']);
		 $file = $this->tableObject;
		 if (is_object($file->objectArray) && count($file->objectArray)) {
			 $file = $file->objectArray;
			 $fileProjects = $this->tableObject->getFileProjects($this->idTableObject);
			 self::$TPL->assign('fileProjects', $fileProjects);
			 $this->addBreadcrumb(t(T_FORM_BUTTON, 'edit') . ' &quot;' . $this->tableObject->objectArray->gsf_file_name . '&quot;', '?action=addEdit&id=' . $this->idTableObject, true);
		 } else {
			 $this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit', true);
		   }
			$allMedia = $this->tableObject->getEnumValues('gsf_media');
			self::$TPL->assign('file', $file);
			self::$TPL->assign('allMedia', $allMedia);

			$allProjects = Db_Projects::getAllProjects();
			self::$TPL->assign('allProjects', $allProjects);

			self::$TPL->setBodyTemplate('general_skin_files_edit');
			return array(
				'type' => 'display'
			);

    }

    public function deleteAction() {
		perm('GENERAL_SKIN_FILES_EDIT', self::$PAGE['p_url']);
        $this->message = t(T_FORM_MSG, 'success');
        if (!Db_Cms_GeneralSkinFiles::abstractDelete($this->idTableObject)) {
            $this->error = true;
            $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
        }
        return array(
            'type' => 'redirect',
            'query' => false
        );
    }

    public function saveAction() {
		perm('GENERAL_SKIN_FILES_EDIT', self::$PAGE['p_url']);
        $skinFileType = strtolower(post('sf_type', 'string'));
        $skinFileSpecialContent = post('sf_special_content', 'int');
        $this->message = t(T_FORM_MSG, 'success');

        if ($skinFileSpecialContent == 1) {
            $skinFileName = post('sf_name', 'string', true);
        } else {
            $skinFileName = post('sf_name', 'string');
        }

        $fileType =  $this->tableObject->objectArray->gsf_file_type;

        if (!empty($skinFileName) && !empty($skinFileType)) {
            $this->tableObject->gsf_file_name = $skinFileName;
            $this->tableObject->gsf_file_type = $skinFileType;
            $this->tableObject->gsf_special_content = $skinFileSpecialContent;
            $this->tableObject->gsf_media = strtolower(post('sf_media', 'string'));

            if ($this->tableObject->save()) {

				$selectedProjects = post('projects', 'array');
				$fields = array();
				$this->tableObject->clearFileProjects($this->tableObject->id);
				if (is_array($selectedProjects) && count($selectedProjects)) {
					foreach ($selectedProjects AS $projectId) {
						$fields[] = array(
							'gsfp_file_id' => $this->tableObject->id,
							'gsfp_project_id' => $projectId
						);
					}
					self::$DB->insertBatch(TAB_GENERAL_SKIN_FILE_PROJECTS, $fields, true);
				}

                if( $fileType != $skinFileType) {
                    $this->tableObject->positionCommonFields = array(
                         'gsf_file_type' => $skinFileType
                    );
                    if(!$this->tableObject->positionSetLast() || !$this->tableObject->positionCleanUp()) {
                        $this->error = true;
                        $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
                    }
                }
            } else {
                $this->error = true;
                $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
            }
        } else {
            $this->error = true;
            $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
        }
        return array(
            'type' => 'redirect',
            'query' => false
        );
    }

    public function saveGeneralSkinFilesPositionAction() {
        $btnAction = post('btnAction', 'string');
		var_dump($this->tableObject->objectArray->gsf_file_type);
		var_dump(post('id'));

        $this->tableObject->positionCommonFields = array(
            'gsf_file_type' => $this->tableObject->objectArray->gsf_file_type
        );
        $this->moveUpDownAjax($btnAction, $this->tableObject);
    }
}
